package com.east.manager;

import com.east.dto.DicValueDTO;
import com.east.entity.DicValue;
import com.east.entity.Product;
import com.east.mapper.DicValueMapper;
import com.east.mapper.ProductMapper;
import com.east.utils.Constants;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import jakarta.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class CustomCache {
    @Resource
    private DicValueMapper dicValueMapper;
    @Resource
    private ProductMapper productMapper;
    /**
     * 用来缓存字典数据
     * 用来缓存 意向商品数据
     */
    public static final Cache<String, Object> CACHE = Caffeine.newBuilder()
            .maximumSize(10_000)
            .build();

    /**
     * 定时任务，向缓存中写入数据，每1分钟执行一次
     */
    @Scheduled(fixedRate = 1, timeUnit = TimeUnit.MINUTES)
    public void fillCache() {
        List<DicValueDTO> dicValues = dicValueMapper.queryAll();
        List<Product> products = productMapper.queryAll();
        CACHE.put(Constants.CACHE_DIC_VALUE, dicValues);
        CACHE.put(Constants.CACHE_PRODUCT_VALUE, products);
    }
}
